package br;

import br.entity.Admin;
import br.entity.Good;
import br.entity.User;

import java.util.ArrayList;
import java.util.Scanner;

import static java.lang.System.exit;

public class Main {

    static int st = 0;//状态
    static User cur = null;

    static ArrayList<Good> goods = new ArrayList<Good>();
    static ArrayList<User> users = new ArrayList<User>();
    static ArrayList<Admin> admins = new ArrayList<Admin>();
    static Scanner sc = new Scanner(System.in);


    public static void Menu() {
        System.out.println("*****欢迎使用电子商城*****");
        System.out.println("     1.注册");
        System.out.println("     2.登录");
        System.out.println("     3.查看商城");
        System.out.println("     4.查看我购买的商品");
        System.out.println("     5.管理员登录");
        System.out.println("     6.退出系统");
        System.out.println("***********************");
        System.out.println("请选择你需要操作：");
    }

    public static void Register() {

        System.out.println("请输入用户名（长度不能小于三位！）：");
        String name = sc.next();
        while (name.length() < 3) {
            System.out.println("用户名长度小于三位请重新输入！");
            name = sc.next();
        }

        System.out.println("请输入密码（密码必须有数字和字母，长度不能小于六位！）：");
        String password = sc.next();
        while (!passwordCheck(password)) {
            password = sc.next();
        }

        String again;
        System.out.println("请再次输入你的密码以确认！");
        again = sc.next();
        while (!again.equals(password)) {
            System.out.println("输入的确认密码与第一次不同，请重新输入！");
            again = sc.next();
        }

        User user = new User(name, password);
        users.add(user);

        System.out.println("注册成功！");

        System.out.println(users);

    }

    public static void Login() {
        if (users.size() < 1) {
            System.out.println("数据库中无用户数据，请进行注册后在登录！");
            return;
        }

        String name;
        String password;
        System.out.println("请输入要登录的用户名：");
        name = sc.next();
        System.out.println("请输入密码：");
        password = sc.next();

        boolean yes = false;
        for (User u : users) {
            if (u.getName().equals(name) && u.getPassword().equals(password)) {
                yes = true;
            }
        }
        if (yes) {
            System.out.println("登录成功！欢迎您！");
            cur = new User(name, password);
            st = 1;
        } else {
            System.out.println("用户不存在或密码错误！");
        }
    }

    public static void Shop() {

    }

    public static void hadBuy() {

    }

    public static void loginAdmin() {

    }

    public static void loginOut(){
        cur = null;
        st = 0;
    }

    public static boolean passwordCheck(String password) {
        if (password.length() < 3) {
            System.out.println("用户名长度小于三位请重新输入！");
            return false;
        }
        boolean haveNum = false;
        boolean haveWord = false;
        for (int i = 0; i < password.length(); i++) {
            if ((password.charAt(i) >= '0' && password.charAt(i) <= '9')) {
                haveNum = true;
            } else if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z') {
                haveWord = true;
            } else if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z') {
                haveWord = true;
            }
        }
        if (!haveNum || !haveWord) {
            System.out.println("输入的密码不合规范：必须有数字和字母，长度不能小于六位！请重新输入！");
            return false;
        }
        return true;
    }

    public static void Options(int op) {
        if (st == 0) {
            switch (op) {
                case 1 -> {
                    System.out.println("你选择的操作是：注册");
                    Register();
                }
                case 2 -> {
                    System.out.println("你选择的操作是：登录");
                    Login();
                }
                case 3 -> {
                    System.out.println("你选择的操作是：查看商城");
                    Shop();
                }
                case 4 -> {
                    System.out.println("你选择的操作是：查看我购买的商品");
                    hadBuy();
                }
                case 5 -> {
                    System.out.println("你选择的操作是：管理员登录");
                    loginAdmin();
                }
                case 6 -> {
                    System.out.println("你选择的操作是：退出系统");
                    System.out.println("已退出系统，感谢您的使用......");
                    exit(0);
                }
                default -> System.out.println("错误的选项，请重新输入！");

            }
        }else if(st ==1){
            switch (op) {
                case 1 -> {
                    System.out.println("你选择的操作是：查看商城");
                    Shop();
                }
                case 2 -> {
                    System.out.println("你选择的操作是：查看我购买的商品");
                    hadBuy();
                }
                case 3 -> {
                    System.out.println("你选择的操作是：退出登录");
                    loginOut();
                }
            }
        }else if(st ==2){

        }
    }

    public static void pM() {
        if (st == 0) {//无登录
            Menu();
        } else if (st == 1) {//用户
            cur.Menu();
        } else if (st == 2) {//管理员

        }
    }

    public static void main(String[] args) {
        //初始化一个管理员账号
        Admin admin = new Admin("admin", "123456");
        admins.add(admin);
        Menu();
        while (true) {
            int op = sc.nextInt();
            Options(op);
            pM();
        }
    }
}